//
//  VisitorLoginView.swift
//  01_新浪微博
//
//  Created by 黄蓉 on 15/12/13.
//  Copyright © 2015年 黄蓉. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate :NSObjectProtocol {
    func resigsterButtonClick()
    func loginButtonClick()
}




class VisitorLoginView: UIView {
//delegate属性为weak,且协议必须遵守NSObjectProtocol协议
   weak var delegate : VisitorLoginViewDelegate?
    
    //根据页面信息,设置页面相关属性,
    func  setupViewInfo(iconImageViewImageName:String, textLabelText:String, isHome:Bool) {
        homeView.image = UIImage(named: iconImageViewImageName)
        textLabel.text = textLabelText
         iconView.hidden = !isHome
           iconView.hidden ? sendSubviewToBack(iconView) : bringSubviewToFront(homeView)
        
        
    }
    //设置首页旋转动画
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.toValue = M_PI * 2
//        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
   override init(frame: CGRect) {
        super.init(frame: frame)
//    print(frame)0000
        setUI()
    
    
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    
    
    func setUI() {
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeView)
        
        addSubview(textLabel)
        addSubview(resigerButton)
        addSubview(loginButton)
        //圆圈
        iconView.translatesAutoresizingMaskIntoConstraints = false
        //xy居中
       addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        //房子,xy居中
        homeView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        //文字.x居中,距离房子底部60,长宽
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 60))
        
        addConstraint( NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subviews(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subviews":textLabel]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subviews(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subviews":textLabel]))
        

        
        
        
        //注册按钮:文字底部10,与文字左对齐,自己的宽高
        resigerButton.translatesAutoresizingMaskIntoConstraints = false
        
                addConstraint( NSLayoutConstraint(item: resigerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
                addConstraint( NSLayoutConstraint(item: resigerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
                addConstraint( NSLayoutConstraint(item: resigerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 100))
               addConstraint( NSLayoutConstraint(item: resigerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 40))
        
        //登录按钮:文字底部10,与文字右对齐,自己的宽高
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        
        addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 100))
        ///  <#Description#>
        ///
        ///  @param loginButton              需要约束的控件
        ///  @param NSLayoutAttribute.Height 约束属性
        ///  @param NSLayoutRelation.Equal   约束条件
        ///  @param nil                      参照对象
        ///  @param NSLayoutAttribute.Height 参照属性
        ///  @param 1.0                      约束倍数
        ///  @param 40                       约束值
        ///
        ///  @return <#return value description#>
        addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 40))
        
       //遮罩
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        
        ///  <#Description#>
        ///
        ///  @param "V:|[subviews]-200-|"            约束属性:V高,H宽,|左右边距距离|,[subviews]名称
        ///  @param NSLayoutFormatOptions.AlignAllBaseline
        ///  @param nil                                    nil
        ///  @param                                        ["名称":实际控件]
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subviews]-200-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subviews":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subviews]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subviews":maskIconView]))
         backgroundColor = UIColor(white: 237/255.0, alpha: 1)  
        
    }
    
    
    
    
    lazy var iconView: UIImageView = {
        
        let iconViewImageName = UIImage(named: "visitordiscover_feed_image_smallicon")
    
        let iconImageView = UIImageView(image: iconViewImageName)
        
//        iconImageView.center = center
        return iconImageView
    }()
    
    lazy var homeView: UIImageView = {
        let homeImageName = UIImage(named: "visitordiscover_feed_image_house")
        
        let homeImageView = UIImageView(image: homeImageName)
        return homeImageView
    }()
    
    lazy var maskIconView: UIImageView = {
        
        let maskImageName = UIImage(named: "visitordiscover_feed_mask_smallicon")
    
        let maskImageView = UIImageView(image: maskImageName)
       
        return maskImageView
    }()
    
    lazy var textLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    
    
    lazy var resigerButton: UIButton = UIButton(title: "注册", normalImageName: "common_button_white_disable",  color: UIColor.orangeColor(), target:self, action: "resigster")
  
    
    lazy var loginButton: UIButton = UIButton(title: "登录", normalImageName: "common_button_white_disable", color: UIColor.lightGrayColor(),target:self,  action: "login")

    func resigster() {
        print("注册")
       delegate?.resigsterButtonClick()
    }
    func login() {
        print("登录")
        delegate?.loginButtonClick()
    }
    
}
